package cn.hncu;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;

import javax.swing.border.Border;

public class Calculation{
    double number1, number2;//记录运算的两个数字
    String operator;
    boolean unfinishedReading;//true为第一个数字未完成阅读，false为已完成阅读
    Frame frame = new Frame("李照福的计算器！");
    TextField inputBox = new TextField(30);
    Button[] button = new Button[20];
    Calculation() {
        //设置输入文本框位置
        frame.setBounds(800, 400, 500, 420);
        frame.add(inputBox, BorderLayout.NORTH);
        //+设置按钮布局
        Panel panel = new Panel();
        panel.setLayout(new GridLayout(5, 4));
        //设置按钮，并将按钮加入panel
        String[] s = {"sqrt", "<-", "AC","%", "7", "8", "9", "+", "4", "5", "6", "-", "1", "2", "3", "*", "0", ".", "=", "/"};
        for (int i = 0; i < s.length; i++) {
            button[i] = new Button(s[i]);
            button[i].addActionListener(new ActionListen());
            panel.add(button[i]);
        }
        frame.add(panel,BorderLayout.CENTER);//将panel设置在中部区域
        frame.setVisible(true);//设置窗口可见
        frame.addWindowListener(new WindowAdapter() {//窗口监听器，注册监听，可以关闭窗口
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
    }
    public class ActionListen implements ActionListener{
        @Override
        public void actionPerformed(ActionEvent e) {//获取事件源的标签
            String label = e.getActionCommand();
            if (label.equals("=")) {
                number2 = Double.parseDouble(inputBox.getText());
                unfinishedReading = true;
                switch (operator) {
                    case "+":
                        inputBox.setText(number1 + number2 + "");
                        break;
                    case "-":
                        inputBox.setText(number1 - number2 + "");
                        break;
                    case "*":
                        inputBox.setText(number1 * number2 + "");
                        break;
                    case "/":
                        inputBox.setText(number1 / number2 + "");
                        break;
                    case "%":
                        inputBox.setText(number1 % number2 + "");
                        break;
                    case "sqrt":
                        inputBox.setText( Math.sqrt(number1) + "");
                        break;
                    case "^2":
                        inputBox.setText(number2 * number2 + "");
                        break;
                }
            } else if (label.equals("+") || label.equals("*") || label.equals("/") || label.equals("%") || label.equals("sqrt")) {
                
            	number1 = Double.parseDouble(inputBox.getText());
                  operator = e.getActionCommand();
                unfinishedReading = false;  
            } else if (label.equals("-")) {
                if (!unfinishedReading) {
                    inputBox.setText("-");
                    unfinishedReading = true;
                } else {
                    number1 = Double.parseDouble(inputBox.getText());
                    operator = e.getActionCommand();
                    unfinishedReading = false;
                }
            } else if (label.equals("<-")) {
                String expression = inputBox.getText();
                if (expression.length() != 0) {
                    expression = expression.substring(0, expression.length() - 1);
                    inputBox.setText(expression);
                } 
            } else if (label.equals("AC")) {
                inputBox.setText("");
                unfinishedReading = false;
            } else {
                String s = inputBox.getText();
                if (!unfinishedReading)
                    inputBox.setText(label);
                else
                    inputBox.setText(s + label);
                unfinishedReading = true;
            }
        }
    }
    public static void main(String[] args) {
        new Calculation();
    }
}

























